package com.link.base.base.staticvalue.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.staticvalue.dao.mybatis.mapper.StaticvalueMapper;
import com.link.base.base.staticvalue.model.Staticvalue;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.service.ServiceException;

import javax.annotation.Resource;

import com.link.core.util.UserUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 */
@Component
public class StaticvalueServiceImpl implements StaticvalueService {
    @Resource
    private StaticvalueMapper staticvalueMapper;

    @Override
    public List<Staticvalue> queryAll(Staticvalue staticvalue) throws Exception {
        BasicModel b = new BasicModel();
        CoreUser user = UserUtil.getUser();
        // 如果用户不存在,则根据前端传的corpid做查询
        if (user == null) {
            if (staticvalue != null) {
                b.setLoginCorpId(staticvalue.getUsercorpid());
            }
        }
        return staticvalueMapper.queryStaticvalue(b);
    }

    @Override
    public Staticvalue getLovByNameAndType(String type, String val) throws ServiceException {

        Staticvalue record = new Staticvalue();
        record.setVal(val);
        record.setType(type);
        try {
            return staticvalueMapper.queryLovByValAndType(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public String getLovValue(String type, String name) throws Exception {
        Staticvalue record = new Staticvalue();
        record.setName(name);
        record.setType(type);
        try {
            Staticvalue result = staticvalueMapper.queryLovByNameAndType(record);
            if (result == null) {
                throw new ServiceException("STATICVALUE-001", type, name);
            }
            return result.getVal();
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
    }


    @Override
    public String getLovName(String type, String value) throws Exception {
        Staticvalue record = new Staticvalue();
        record.setVal(value);
        record.setType(type);
        try {
            Staticvalue result = staticvalueMapper.queryLovByValAndType(record);
            if (result == null) {
                throw new ServiceException("STATICVALUE-002", type, value);
            }
            return result.getName();
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public List<Staticvalue> queryByLovType(Staticvalue record) throws Exception {
        return staticvalueMapper.queryByLovType(record);
    }

    @Override
    public List<Staticvalue> queryByLovTypes(Staticvalue record) throws Exception {
        return staticvalueMapper.queryByLovTypes(record);
    }

    /**
     * 类型查询
     *
     * @param staticvalue
     * @return
     * @throws Exception
     */
    @Override
    public List<Staticvalue> queryTypes(Staticvalue staticvalue) throws Exception {
        return staticvalueMapper.queryTypes(staticvalue);
    }

    /**
     * 根据类型分组
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, List<Staticvalue>> queryLovGroupByTypes(Staticvalue staticvalue) throws Exception {
        Map<String, List<Staticvalue>> result = new HashMap<>();
        // 查询全部值列表
        List<Staticvalue> staticvalues = queryAll(staticvalue);
        List<Staticvalue> staticvalueList = null;
        if (staticvalues != null && staticvalues.size() > 0) {
            for (Staticvalue temp : staticvalues) {
                if (temp.getType() != null) {
                    // 根据类型做分组
                    if (result.containsKey(temp.getType())) {
                        staticvalueList = result.get(temp.getType());
                    } else {
                        staticvalueList = new ArrayList<>();
                    }
                    staticvalueList.add(temp);
                    result.put(temp.getType(), staticvalueList);
                }
            }
        }
        return result;
    }

}

